package com.azt.front.action.freemarker;

import com.azt.api.pojo.Content;
import com.azt.api.pojo.FriendlyLink;
import com.azt.api.service.ContentService;
import com.azt.api.service.FriendlyLinkService;
import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 前台底部
 * */
@Component("front_foot")
public class FootDirective implements TemplateDirectiveModel {

    @Autowired
    private FriendlyLinkService friendlyLinkService;

    @Autowired
    private ContentService contentService;

    @Override
    public void execute(Environment env, Map params, TemplateModel[] loopVars,
                        TemplateDirectiveBody body)
            throws TemplateException, IOException {

        //合作机构&友情链接
        List<FriendlyLink> friendlyLinkList = friendlyLinkService.findIndexFriendlyLink();
        Map parmap = new HashMap();

        List<FriendlyLink> hzjgList = new ArrayList<>();    //合作机构
        List<FriendlyLink> yqljList = new ArrayList<>();    //友情链接
        for(FriendlyLink friendlyLink : friendlyLinkList) {
            if(friendlyLink.getType() != null) {
                 if(friendlyLink.getType().intValue() == 1) {
                     yqljList.add(friendlyLink);
                 } else if(friendlyLink.getType().intValue() == 2) {
                     hzjgList.add(friendlyLink);
                 }
            }
        }

        parmap.put("hzjgList", hzjgList);
        parmap.put("yqljList", yqljList);

        //帮助中心热门问题
        List<Content> xsznList = contentService.findHotTips("OFTEN_PRO", 5);   //常见问题
        List<Content> cgsrmList = contentService.findHotTips("PURCHASER_DOOR", 5);   //采购商入门
        List<Content> gysrmList = contentService.findHotTips("PROVIDER_DOOR", 5);   //供应商入门
        List<Content> ddglList = contentService.findHotTips("SHOPPING_DIR", 5);   //购物指南

        parmap.put("xsznList", xsznList);
        parmap.put("cgsrmList", cgsrmList);
        parmap.put("gysrmList", gysrmList);
        parmap.put("ddglList", ddglList);

        DirectiveUtils.writer(parmap, env, params, body);
    }

}
